package com.jmm.common.utils;

import com.jmm.api.dao.user.ApiAppUserMapper;
import com.jmm.api.pojo.domain.user.ApiAppUserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class ShiroUtils {
	public static Subject getSubjct() {
		return SecurityUtils.getSubject();
	}

	// api
	private final static ApiAppUserMapper apiAppUserMapper = SpringContextHolder.getBean(ApiAppUserMapper.class);
	public static ApiAppUserDO getAppUserDO() {
		String jwt = (String)getSubjct().getPrincipal();
		if(jwt == null){
			return null;
		}
		String userId = JWTUtil.getUserId(jwt);
		return apiAppUserMapper.selectById(userId);
	}

	public static void logout() {
		getSubjct().logout();
	}

	public static String getUserId(){
		String jwt = (String)getSubjct().getPrincipal();
		if(jwt != null){
			return JWTUtil.getUserId(jwt);
		}
		return null;
	}
}
